package com.itheima.controller;

import com.itheima.pojo.Dept;
import com.itheima.pojo.Result;
import com.itheima.service.DeptService;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 部门管理Controller
 */
@Slf4j
@RestController
@RequestMapping("/depts")
public class DeptController {
//日志记录对象
//    private static Logger log = LoggerFactory.getLogger(DeptController.class);
//    注入service注解
    @Autowired
    private DeptService deptService;
//    @RequestMapping(value = "/depts" , method = RequestMethod.GET)
//    查询
    @GetMapping
    public Result list(){
        log.info("查询全部部门数据");
//        调用service查询部门数据
//        List<Dept>deptService.list();
        List<Dept> deptList = deptService.list();
        return Result.success(deptList);
    }

//    删除
    @DeleteMapping("/{id}")
   public Result delete(@PathVariable Integer id){
        log.info("删除id");
        deptService.delete(id);
        return Result.success();
   }

//   新增
    @PostMapping
    public Result add(@RequestBody  Dept dept){
        log.info("新增部门：{}",dept);
//        调用
        deptService.add(dept);
        return Result.success();

    }

//    根据id查询
    @GetMapping("/{id}")
    public Result selectid (@PathVariable Integer id){
        log.info("根据id查询");
        Dept dept = deptService.getByid(id);
        return Result.success(dept);
    }

//    修改
    @PutMapping
    public Result update(@RequestBody Dept dept){
        log.info("修改",dept);
        deptService.putdept(dept);
        return Result.success();
    }
}
